package com.memory.modules.business.service.sell.impl;

import com.memory.common.data.PageSort;
import com.memory.modules.business.domain.earn.AccountEarn;
import com.memory.modules.business.domain.earn.AccountEarnRecord;
import com.memory.modules.business.domain.sell.Sell;
import com.memory.modules.business.domain.user.Account;
import com.memory.modules.business.repository.earn.AccountEarnRecordRepository;
import com.memory.modules.business.repository.earn.AccountEarnRepository;
import com.memory.modules.business.repository.sell.SellRepository;
import com.memory.modules.business.repository.user.AccountRepository;
import com.memory.modules.business.service.earn.AccountEarnRecordService;
import com.memory.modules.business.service.earn.AccountEarnService;
import com.memory.modules.business.service.sell.SellService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @PackageName: com.memory.modules.sell.service.impl
 * @ClassName: SellRepository
 * @Description:
 * @author: zhuguangxun
 * @date 2021/3/10
 */
@Service
@RequiredArgsConstructor
public class SellServiceImpl implements SellService {

    private final SellRepository sellRepository;

    private final AccountRepository accountRepository;

    private final AccountEarnService accountEarnService;

    private final AccountEarnRecordService accountEarnRecordService;


    @Override
    @Transactional
    public Sell create(Sell sell) {
        final Sell save = sellRepository.save(sell);
        accountEarnRecordService.createAccountEarnRecord(sell.getAccountId(), sell.getCount(), sell.getPrice(), sell.getCreateDate());
        accountEarnService.saveAccountEarn(sell.getAccountId(), sell.getCount(), sell.getPrice(), sell.getCreateDate());
        return sell;
    }


    @Override
    public Sell update(Long id, Sell sell) {
        sell.setId(id);
        return this.findById(id).map(e ->
                sellRepository.save(sell)
        ).orElseThrow(() -> new RuntimeException("未获取用户信息"));
    }

    @Override
    public void remove(Long id) {
        this.findById(id).ifPresent(e -> sellRepository.deleteById(e.getId()));
    }

    @Override
    public Optional<Sell> findById(Long id) {
        return sellRepository.findById(id);
    }

    @Override
    public Page<Sell> findList(Example<Sell> example) {
        final PageRequest pageRequest = PageSort.pageRequest();
        final Page<Sell> all = sellRepository.findAll(example, pageRequest);
        final Set<Long> collect = all.getContent().stream().map(Sell::getAccountId).collect(Collectors.toSet());
        final Map<Long, String> collect1 = accountRepository.findAccountByIdIn(collect).stream().collect(Collectors.toMap(Account::getId, Account::getRealName, (e1, e2) -> e2));
        all.getContent().stream().forEach(e -> e.setAccountName(collect1.get(e.getAccountId())));
        return all;
    }

    @Override
    public List<Sell> findListNoPage(Example<Sell> example) {
        return sellRepository.findAll(example);
    }

    @Override
    public List<Sell> findByAccountId(Long accountId) {
        return sellRepository.findByAccountId(accountId);
    }
}
